package com.ccp.dev.im.controller;

import com.ccp.dev.core.basic.internationalization.SysBaseController;
import com.ccp.dev.core.basic.consts.UrlConstants;
import com.ccp.dev.core.basic.response.ResultData;
import com.ccp.dev.core.basic.web.query.QueryFilter;
import com.ccp.dev.core.util.BeanUtils;
import com.ccp.dev.core.util.RequestUtil;
import com.ccp.dev.im.model.ImRoom;
import com.ccp.dev.im.model.ImRoomUser;
import com.ccp.dev.im.server.constant.Constants;
import com.ccp.dev.im.service.ImRoomService;
import com.ccp.dev.im.service.ImRoomUserService;
import com.ccp.dev.core.util.log.SysAuditThreadLocalHolder;
import com.ccp.dev.core.basic.annotion.Action;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.ModelMap;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 群聊成员 信息操作处理控制器
 *
 * @author y
 * @date 2020-02-17 11:39:36
 */
@Controller
/**
 * 请将此名称添加到日志开关
 */
@Action(customModel = "群聊成员")
@RequestMapping("/im/imRoomUser")
public class ImRoomUserController extends SysBaseController {
    @Resource
    private ImRoomUserService imRoomUserService;
    @Resource
    private ImRoomService imRoomService;


    /**
     * 功能描述: 根据群id查询所有的群聊成员
     *
     * @param: [request] http请求
     * @return: java.lang.Object
     * @auther: y
     * @date: 2020-02-17 11:39:36
     */
    @RequestMapping(UrlConstants.MODULE_FUNCTION_LIST)
    @ResponseBody
    @Action(description = "根据群id查询所有的群聊成员", detail = "根据群id查询所有的群聊成员<#if success>成功<#else></#if>")
    public Object list(HttpServletRequest request) {
        QueryFilter queryFilter = new QueryFilter(request, false);
        Map<String, Object> retMap;
        try {
            String roomId = RequestUtil.getString(request, "id");
            retMap = imRoomUserService.getRoomUserInfoByRoomId(roomId, queryFilter);
            SysAuditThreadLocalHolder.putParamerter("success", true);
        } catch (Exception e) {
            SysAuditThreadLocalHolder.putParamerter("success", false);
            return getResultDataError("query.failure");
        }
        return getResultDataSuccess("query.success", retMap);
    }

    /**
     * 功能描述: 跳转到新增页面
     *
     * @return: java.lang.String
     * @auther: y
     * @date: 2020-02-17 11:39:36
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + UrlConstants.MODULE_FUNCTION_ADD_UPDATE)
    @Action(description = "跳转到群聊成员新增页面", exectype = "页面跳转日志")
    public String viewAdd(HttpServletRequest request, ModelMap modelMap) {
        String roomId = RequestUtil.getString(request, "roomId");
        modelMap.put("roomId", roomId);
        return "/functionPage/groupfun/groupaddpen";
    }

    /**
     * 功能描述: 跳转到新增页面
     *
     * @return: java.lang.String
     * @auther: y
     * @date: 2020-02-17 11:39:36
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + UrlConstants.MODULE_FUNCTION_DELETE)
    @Action(description = "跳转到群聊成员删除页面", exectype = "页面跳转日志")
    public String viewDel(HttpServletRequest request, ModelMap modelMap) {
        String roomId = RequestUtil.getString(request, "roomId");
        modelMap.put("roomId", roomId);
        return "/functionPage/groupfun/groupdelpen";
    }

    /**
     * 群聊里面邀请人员
     *
     * @param imRoomUser 对象
     * @return ResultData
     */
    @RequestMapping("/addRoomUsers")
    @ResponseBody
    @Action(description = "群聊里面邀请人员", detail = "群聊里面邀请人员<#if success>成功<#else>失败</#if>")
    public ResultData addRoomUsers(@RequestBody ImRoomUser imRoomUser) {
        try {
            ImRoom imRoom = imRoomService.getOneById(imRoomUser.getRoomId());
            if (BeanUtils.isEmpty(imRoom)) {
                SysAuditThreadLocalHolder.putParamerter("success", false);
                return getResultDataError(Constants.CUSTOME_ROOM_ERROR, "addDelRoomUsers.nullRoom");
            }
            //查询当前用户是否在这个群聊内
            boolean userIsExistCurRoom = imRoomUserService.qryCurrentUserExistOneRoom(imRoom.getId());
            if (!userIsExistCurRoom) {
                SysAuditThreadLocalHolder.putParamerter("success", false);
                return getResultDataError(Constants.CUSTOME_ROOM_ERROR, "updateRoom.noUser");
            }
            imRoomUserService.addRoomUsers(imRoomUser.getRoomId(), imRoomUser.getUserIds(),imRoomUser.getAddUserNames(),false);
        } catch (Exception e) {
            e.printStackTrace();
            SysAuditThreadLocalHolder.putParamerter("success", false);
            return getResultDataError("operation.failure");
        }
        SysAuditThreadLocalHolder.putParamerter("success", true);
        return getResultDataSuccess("operation.success");
    }

    /**
     * 群聊里面删除人员
     *
     * @param imRoomUser 对象
     * @return ResultData
     */
    @RequestMapping("/delRoomUsers")
    @ResponseBody
    @Action(description = "群聊里面删除人员", detail = "群聊里面删除人员<#if success>成功<#else>失败</#if>")
    public ResultData delRoomUsers(@RequestBody ImRoomUser imRoomUser) {
        try {
            ImRoom imRoom = imRoomService.getOneById(imRoomUser.getRoomId());
            if (BeanUtils.isEmpty(imRoom)) {
                SysAuditThreadLocalHolder.putParamerter("success", false);
                return getResultDataError(Constants.CUSTOME_ROOM_ERROR, "addDelRoomUsers.nullRoom");
            }
            //查询当前用户是否在这个群聊内
            boolean userIsExistCurRoom = imRoomUserService.qryCurrentUserExistOneRoom(imRoom.getId());
            if (!userIsExistCurRoom) {
                SysAuditThreadLocalHolder.putParamerter("success", false);
                return getResultDataError(Constants.CUSTOME_ROOM_ERROR, "updateRoom.noUser");
            }
            imRoomUserService.delRoomUsers(imRoomUser.getRoomId(), imRoomUser.getUserIds(), imRoom.getRoomName(),imRoomUser.isSignOut());
        } catch (Exception e) {
            e.printStackTrace();
            SysAuditThreadLocalHolder.putParamerter("success", false);
            return getResultDataError("operation.failure");
        }
        SysAuditThreadLocalHolder.putParamerter("success", true);
        return getResultDataSuccess("operation.success");
    }
}
